package com.slipper.system.employee.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.slipper.common.exception.CommonException;
import com.slipper.common.transaction.ReadTransactional;
import com.slipper.common.transaction.WriteTransactional;
import com.slipper.common.util.MD5;
import com.slipper.common.util.StringUtil;
import com.slipper.common.util.Tool;
import com.slipper.system.api.employee.bo.EmployeeBO;
import com.slipper.system.employee.dao.EmployeeDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@ReadTransactional
public class EmployeeService {
    @Resource
    private EmployeeDao employeeDao;

    public EmployeeBO queryBy(String employeeId){
        return employeeDao.queryBy(employeeId);
    }

    public EmployeeBO queryBy(EmployeeBO bo){
        return employeeDao.queryBy(bo.getId());
    }

    public List<EmployeeBO> queryList(EmployeeBO bo) {
        return employeeDao.queryList(bo);
    }

    public List<EmployeeBO> queryListWithOrg(EmployeeBO bo) {
        return employeeDao.queryListWithOrg(bo);
    }

    public PageInfo<EmployeeBO> queryPage(EmployeeBO bo){
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(employeeDao.queryList(bo));
    }

    @WriteTransactional
    public int save(EmployeeBO bo) {
        if (!StringUtil.isBlank(bo.getUsername()) && this.employeeDao.queryUseBy(bo) > 0) {
            throw new CommonException("登录账号[" + bo.getUsername()  + "]已经被使用！");
        }

        int result;
        if (!StringUtil.isBlank(bo.getPassword())) {
            bo.setPassword(MD5.encode(bo.getPassword()));
        }
        if (bo.getId() != null && !"".equals(bo.getId())) {
            result = employeeDao.update(bo);
        } else {
            bo.setId(Tool.getUUID());
            result = employeeDao.insert(bo);
        }

        result += this.employeeDao.deleteEmployeeRole(bo.getId());
        if (bo.getRoleIds() != null) {
            for (String roleId : bo.getRoleIds().split(",")) {
                result += this.employeeDao.saveEmployeeRole(Tool.getUUID(), bo.getId(), roleId);
            }
        }

        result += this.employeeDao.deleteEmployeeOrganization(bo.getId());
        Set<String> orgIdSet = new HashSet<>();
        if (bo.getOrganizationId() != null) {
            orgIdSet.add(bo.getOrganizationId());
        }
        if (bo.getOrganizationIds() != null) {
            Collections.addAll(orgIdSet, bo.getOrganizationIds());
        }
        orgIdSet.forEach(orgId -> this.employeeDao.saveEmployeeOrganization(Tool.getUUID(), bo.getId(), orgId));

        return result;
    }

    @WriteTransactional
    public Integer updatePassword(EmployeeBO bo) {
        bo.setPassword(MD5.encode(bo.getPassword()));
        return this.employeeDao.updatePassword(bo);
    }

    @WriteTransactional
    public int delete(EmployeeBO bo) {
        String[] ids = null;
        if (bo.getId() != null && !"".equals(bo.getId())) {
            ids = new String[] { bo.getId() };
        }
        if (bo.getIds() != null && bo.getIds().length > 0) {
            ids = bo.getIds();
        }
        if (ids != null) {
            for (String id : ids) {
                EmployeeBO employee = employeeDao.queryBy(id);
                if (!StringUtil.isBlank(employee.getUsername())) {
                    throw new CommonException(employee.getName() + "已分配登录账号，不允许删除！");
                }
            }
        }

        return employeeDao.delete(bo);
    }

    public EmployeeBO queryByLogin(EmployeeBO bo) {
        return employeeDao.queryByLogin(bo);
    }

    @WriteTransactional
    public Integer saveUserRole(EmployeeBO bo) {
        int result = this.employeeDao.deleteEmployeeRole(bo.getId());

        if (bo.getRoleIds() != null) {
            for (String roleId : bo.getRoleIds().split(",")) {
                result += this.employeeDao.saveEmployeeRole(Tool.getUUID(), bo.getId(), roleId);
            }
        }

        return result;
    }

    public List<EmployeeBO> queryByIds(EmployeeBO bo) {
        return this.employeeDao.queryByIds(bo);
    }

    @WriteTransactional
    public Integer updateOrganization(EmployeeBO employee) {
        return this.employeeDao.updateOrganization(employee);
    }
}
